#include "../Logs/jiuqilog.h"
#include <unistd.h>

void test()
{
    std::unique_ptr<jiuqilog::LoggerBuilder> builder =
        std::make_unique<jiuqilog::GlobalLoggerBuilder>();
    builder->buildLoggerFormatter("[%d{%H:%M:%S}][%t][%p][%c][%f:%l]jiuqi:%T%m%n");
    builder->buildLoggerSink<jiuqilog::StdoutSink>();
    builder->buildLoggerSink<jiuqilog::RollSinkBySize>("./logfile/log.log", 1024 * 1024);
    builder->buildLoggerLevel(jiuqilog::LogLevel::value::WARN);
    builder->buildLoggerName("async_logger");
    builder->buildLoggerType(jiuqilog::LoggerType::LOGGER_ASYNC);
    jiuqilog::Logger::ptr logger = builder->build();

    size_t cursize = 0;
    size_t count = 0;
    while (cursize < 500000)
    {
        logger->error("%s-%d", "测试", count++);
        cursize += 10;
        usleep(1000);
    }
}

int main()
{
    // util测试
    // std::cout << bitlog::util::Date::now() << std::endl;
    // std::string pathname = "./abc/bcd/a.txt";
    // bitlog::util::File::create_directory(bitlog::util::File::path(pathname));'

    // level测试
    // std::cout << jiuqilog::LogLevel::toString(jiuqilog::LogLevel::value::DEBUG) << std::endl;

    // format测试
    // jiuqilog::LogMsg msg(jiuqilog::LogLevel::value::DEBUG, 66, "test.cc", "root", "格式化功能测试");
    // jiuqilog::Formatter fmt("[%d{%H:%M:%S}][%t][%p][%c][%f:%l]jiuqi:%T%m%n");
    // fmt.format(std::cout, msg);
    // std::string str = fmt.format(msg);
    // std::cout << str;

    // sink测试
    // jiuqilog::LogSink::ptr lsp;
    // lsp = jiuqilog::SinkFactory::createSink<jiuqilog::StdoutSink>();
    // lsp->log(str.c_str(), str.size());

    // lsp = jiuqilog::SinkFactory::createSink<jiuqilog::FileSink>("./logfile/log.log");
    // lsp->log(str.c_str(), str.size());
    // lsp = jiuqilog::SinkFactory::createSink<jiuqilog::RollSinkBySize>("./logfile/log_roll", 1024 * 1024);
    // while(1)
    // {
    //     lsp->log(str.c_str(), str.size());
    //     usleep(1000);
    // }

    // lsp = jiuqilog::SinkFactory::createSink<jiuqilog::RollSinkByTime>("./logfile/log", jiuqilog::GapType::GAP_MINUTE);
    // while(1)
    // {
    //     lsp->log(str.c_str(), str.size());
    //     sleep(1);
    // }

    // 同步落地日志器测试
    // jiuqilog::LogLevel::value level = jiuqilog::LogLevel::value::WARN;
    // std::string logger_name = "logger";
    // jiuqilog::Formatter::ptr fmt = std::make_shared<jiuqilog::Formatter>("[%d{%H:%M:%S}][%t][%p][%c][%f:%l]jiuqi:%T%m%n");
    // std::vector<jiuqilog::LogSink::ptr> sinks;
    // sinks.push_back(jiuqilog::SinkFactory::createSink<jiuqilog::StdoutSink>());
    // sinks.push_back(jiuqilog::SinkFactory::createSink<jiuqilog::FileSink>("./logfile/log.log"));

    // jiuqilog::Logger::ptr logger = std::make_shared<jiuqilog::SyncLogger>(logger_name, level, fmt, sinks);
    // logger->warn(__FILE__, __LINE__, "%s", "你好");

    // std::unique_ptr<jiuqilog::LoggerBuilder> builder =
    //     std::make_unique<jiuqilog::LocalLoggerBuilder>();
    // builder->buildLoggerFormatter("[%d{%H:%M:%S}][%t][%p][%c][%f:%l]jiuqi:%T%m%n");
    // builder->buildLoggerSink<jiuqilog::StdoutSink>();
    // builder->buildLoggerSink<jiuqilog::FileSink>("./logfile/log.log");
    // builder->buildLoggerLevel(jiuqilog::LogLevel::value::WARN);
    // builder->buildLoggerName("async_logger");
    // builder->buildLoggerType(jiuqilog::LoggerType::LOGGER_ASYNC);
    // builder->buildeEnableUnsafeAsync();

    // std::shared_ptr<jiuqilog::Logger> logger = builder->build();
    // size_t cursize = 0, count = 0;
    // logger->error(__FILE__, __LINE__, "%s-%d", "error", count++);

    // while (cursize < 10 * 1024 * 1024)
    // {
    //     // logger->debug(__FILE__, __LINE__, "%s-%d", "hello", count++);
    //     // std::cout << "cursize:" << cursize << std::endl;
    //     logger->error(__FILE__, __LINE__, "%s-%d", "error", count++);
    //     cursize += 20;
    // }

    // buffer测试
    // std::ifstream ifs("./logfile/log.log", std::ios::binary);
    // if (!ifs.is_open())
    //     return 1;
    // ifs.seekg(0, std::ios::end);
    // size_t fsize = ifs.tellg();
    // ifs.seekg(0, std::ios::beg);
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0], fsize);
    // ifs.close();

    // jiuqilog::Buffer buffer;
    // buffer.push(body.c_str(), fsize);
    // std::cout << fsize << std::endl;
    // std::cout << buffer.readAbleSize() << std::endl;

    // std::ofstream ofs("./logfile/log.log", std::ios::binary | std::ios::app);
    // if (!ofs.is_open())
    //     return 1;
    // // ofs.write(buffer.begin(), buffer.readAbleSize());

    // int n = buffer.readAbleSize();
    // for (int i = 0; i < n; i++)
    // {
    //     ofs.write(buffer.begin(), 1);
    //     buffer.moveRead(1);
    // }

    // loggermanager测试
    // std::unique_ptr<jiuqilog::LoggerBuilder> builder =
    //     std::make_unique<jiuqilog::GlobalLoggerBuilder>();
    // builder->buildLoggerFormatter("[%d{%H:%M:%S}][%t][%p][%c][%f:%l]jiuqi:%T%m%n");
    // builder->buildLoggerSink<jiuqilog::StdoutSink>();
    // builder->buildLoggerSink<jiuqilog::FileSink>("./logfile/log.log");
    // builder->buildLoggerLevel(jiuqilog::LogLevel::value::WARN);
    // builder->buildLoggerName("async_logger");
    // builder->buildLoggerType(jiuqilog::LoggerType::LOGGER_ASYNC);
    // builder->buildeEnableUnsafeAsync();
    // jiuqilog::Logger::ptr logger = builder->build();

    // jiuqilog::Logger::ptr logger = jiuqilog::LoggerManager::getInstance().getLogger("async_logger");
    // logger->error(__FILE__, __LINE__, "%s", "loggermananger测试");

    // 全局接口测试
    // logger->debug("%s-%d", "接口测试", 1);
    // logger->info("%s-%d", "接口测试", 2);
    // logger->warn("%s-%d", "接口测试", 3);
    // logger->error("%s-%d", "接口测试", 4);
    // logger->fatal("%s-%d", "接口测试", 5);

    // DEBUG("%s-%d", "接口测试", 1);
    // INFO("%s-%d", "接口测试", 2);
    // WARN("%s-%d", "接口测试", 3);
    // ERROR("%s-%d", "接口测试", 4);
    // FATAL("%s-%d", "接口测试", 5);
    test();
    return 0;
}